package cn.itcast.service.impl;

import cn.itcast.mapper.StudentMapper;
import cn.itcast.pojo.PageInfo;
import cn.itcast.pojo.QueryString;
import cn.itcast.pojo.Student;
import cn.itcast.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageInfo<Student> findBypage(QueryString queryString) {
        //开启分页
        PageHelper.startPage(queryString.getPage(), queryString.getPageSize());
        //调用dao层方法把学生放到集合里
        List<Student> list = studentMapper.findByPage(queryString);
        log.info("学生:{}", list);
        //封装成page
        Page<Student> page = (Page<Student>) list;
        PageInfo<Student> pageinfo = new PageInfo<>((int) page.getTotal(), page.getResult());
        return pageinfo;
    }

    @Override
    public boolean add() {
        int add = studentMapper.add();
        return add > 0;
    }

    @Override
    public boolean delete(int[] ids) {
        int row = studentMapper.deleteByIds(ids);

        return row > 0;
    }

    @Override
    public Student findById(Integer id) {
        Student student = studentMapper.findById(id);

        return student;
    }

    @Override
    public boolean update(Student student) {
        int row = studentMapper.update(student);

        return row > 0;
    }

    @Override
    public boolean updates(Integer id, Integer score) {
        int row = studentMapper.updates(id, score);

        return row > 0;
    }

}
